namespace QMVnSoft.SystemForms
{
    using QMVnSoft.Controls;
    using QMVnSoft.DAL;
    using System;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Windows.Forms;

    public class FormDmFieldLister : FormControl
    {
        private BtnControl btnCancel;
        private BtnControl btnOk;
        private IContainer components;
        private ColumnHeader FieldName;
        private string is_FieldList;
        private LViewControl lstField;

        public FormDmFieldLister(object pi_Order, int pi_Type, int pi_Top, int pi_Left, string ps_Fields)
        {
            this.InitializeComponent();
            base.Top = pi_Top + (base.Height - base.ClientSize.Height);
            base.Left = pi_Left + (base.Width - base.ClientSize.Width);
            this.FieldList = ps_Fields;
            this.FillData(pi_Order, pi_Type);
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            base.Close();
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            string str = "";
            for (int i = 0; i < this.lstField.Items.Count; i++)
            {
                if (this.lstField.Items[i].Checked)
                {
                    if (string.Empty.Equals(str))
                    {
                        str = str + this.lstField.Items[i].Text;
                    }
                    else
                    {
                        str = str + "," + this.lstField.Items[i].Text;
                    }
                }
            }
            if (!string.Empty.Equals(str))
            {
                this.FieldList = str;
                base.Close();
            }
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (this.components != null))
            {
                this.components.Dispose();
            }
            base.Dispose(disposing);
        }

        private void FillData(object pi_Order, int pi_Type)
        {
            string str = "," + this.FieldList + ",";
            string text = "";
            DataTable table = new DmFileDataAccess().GetTable(pi_Order, pi_Type);
            this.lstField.Items.Clear();
            for (int i = 0; i < table.Rows.Count; i++)
            {
                text = table.Rows[i]["Ma_Field"].ToString();
                this.lstField.Items.Add(text);
                if (str.Contains("," + text + ","))
                {
                    this.lstField.Items[i].Group = this.lstField.Groups[0];
                    this.lstField.Items[i].Checked = true;
                }
                else
                {
                    this.lstField.Items[i].Group = this.lstField.Groups[1];
                }
            }
        }

        private void InitializeComponent()
        {
            ListViewGroup group = new ListViewGroup("Đã chọn", HorizontalAlignment.Left);
            ListViewGroup group2 = new ListViewGroup("Chưa chọn", HorizontalAlignment.Left);
            ListViewItem item = new ListViewItem("MA_VT");
            ListViewItem item2 = new ListViewItem("TEN_VT");
            ListViewItem item3 = new ListViewItem("DVT");
            ListViewItem item4 = new ListViewItem("GIA_MUA");
            ListViewItem item5 = new ListViewItem("GIA_BAN");
            ListViewItem item6 = new ListViewItem("MA_NH_VT");
            ListViewItem item7 = new ListViewItem("MA_LOAI1");
            ListViewItem item8 = new ListViewItem("MA_LOAI2");
            ListViewItem item9 = new ListViewItem("MA_LOAI3");
            this.lstField = new LViewControl();
            this.FieldName = new ColumnHeader();
            this.btnCancel = new BtnControl();
            this.btnOk = new BtnControl();
            base.SuspendLayout();
            this.lstField.Anchor = AnchorStyles.Right | AnchorStyles.Left | AnchorStyles.Bottom | AnchorStyles.Top;
            this.lstField.CheckBoxes = true;
            this.lstField.Columns.AddRange(new ColumnHeader[] { this.FieldName });
            this.lstField.Font = new Font("Arial", 8.25f);
            group.Header = "Đã chọn";
            group.Name = "InUsedGroup";
            group2.Header = "Chưa chọn";
            group2.Name = "NotInUsedGroup";
            this.lstField.Groups.AddRange(new ListViewGroup[] { group, group2 });
            this.lstField.HeaderStyle = ColumnHeaderStyle.None;
            item.Checked = true;
            item.Group = group;
            item.StateImageIndex = 1;
            item2.Checked = true;
            item2.Group = group;
            item2.StateImageIndex = 1;
            item3.Checked = true;
            item3.Group = group;
            item3.StateImageIndex = 1;
            item4.Group = group2;
            item4.StateImageIndex = 0;
            item5.Group = group2;
            item5.StateImageIndex = 0;
            item6.Group = group2;
            item6.StateImageIndex = 0;
            item7.Group = group2;
            item7.StateImageIndex = 0;
            item8.Group = group2;
            item8.StateImageIndex = 0;
            item9.Group = group2;
            item9.StateImageIndex = 0;
            this.lstField.Items.AddRange(new ListViewItem[] { item, item2, item3, item4, item5, item6, item7, item8, item9 });
            this.lstField.Location = new Point(12, 12);
            this.lstField.Name = "lstField";
            this.lstField.Size = new Size(0xe2, 0xe7);
            this.lstField.TabIndex = 1;
            this.lstField.UseCompatibleStateImageBehavior = false;
            this.lstField.View = View.Details;
            this.lstField.ItemCheck += new ItemCheckEventHandler(this.lstField_ItemCheck);
            this.FieldName.Text = "Field name";
            this.FieldName.Width = 200;
            this.btnCancel.Anchor = AnchorStyles.Right | AnchorStyles.Bottom;
            //this.btnCancel.DialogResult = DialogResult.Cancel;
            this.btnCancel.Font = new Font("Arial", 8.25f);
            this.btnCancel.Location = new Point(128, 0x100);
            this.btnCancel.Name = "btnCancel";
            this.btnCancel.Size = new Size(0x4b, 0x17);
            this.btnCancel.TabIndex = 13;
            this.btnCancel.Text = "&Hủy bỏ";
            this.btnCancel.UseVisualStyleBackColor = true;
            this.btnCancel.Click += new EventHandler(this.btnCancel_Click);
            this.btnOk.Anchor = AnchorStyles.Right | AnchorStyles.Bottom;
            this.btnOk.Font = new Font("Arial", 8.25f);
            this.btnOk.Location = new Point(0x2f, 0x100);
            this.btnOk.Name = "btnOk";
            this.btnOk.Size = new Size(0x4b, 0x17);
            this.btnOk.TabIndex = 12;
            this.btnOk.Text = "&Chấp nhận";
            this.btnOk.UseVisualStyleBackColor = true;
            this.btnOk.Click += new EventHandler(this.btnOk_Click);
            base.AutoScaleDimensions = new SizeF(6f, 13f);
            base.CancelButton = this.btnCancel;
            base.ClientSize = new Size(250, 0x123);
            base.ControlBox = false;
            base.Controls.Add(this.btnCancel);
            base.Controls.Add(this.btnOk);
            base.Controls.Add(this.lstField);
            base.Name = "FormDmFieldList";
            base.ShowInTaskbar = false;
            base.StartPosition = FormStartPosition.Manual;
            this.Text = "";
            base.ResumeLayout(false);
        }

        private void lstField_ItemCheck(object sender, ItemCheckEventArgs e)
        {
            if (e.NewValue == CheckState.Checked)
            {
                this.lstField.Items[e.Index].Group = this.lstField.Groups[0];
            }
            else
            {
                this.lstField.Items[e.Index].Group = this.lstField.Groups[1];
            }
        }

        public string FieldList
        {
            get
            {
                return this.is_FieldList;
            }
            set
            {
                this.is_FieldList = value;
            }
        }
    }
}

